// pages/classify/search/search.js
var app = getApp()
import Index from '../../../api/index';
import userIndex from '../../../api/user';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isPhone:app.globalData.isPhone,
    modelPhone:false,
    isIphoneX:app.globalData.isIphoneX,
    headerHeight:app.globalData.headerHeight,

    label:'',
    pageNo:1,
    pageSize:20,
    sort:'',
    title:'',
    typeId:'',

    productList:[],
    isMore:false,

    salesVolume:[
      {
        value:11,
        name:'销量升序'
      },
      {
        value:12,
        name:'销量降序'
      },
    ],
    priceArray:[
      {
        value:21,
        name:'价格升序'
      },
      {
        value:22,
        name:'价格降序'
      },
    ]
  },

  bindPickerChange(e){
    console.log(e)
    var type=e.currentTarget.dataset.sort
    var index=e.detail.value
    var salesVolume=this.data.salesVolume
    var priceArray=this.data.priceArray
    if(type=="sales"){
      this.setData({
        sort:salesVolume[index].value
      })
    }else{
      this.setData({
        sort:priceArray[index].value
      })
    }
    this.getProductList()
  },
  getInput(e){
    console.log(e)
    this.setData({
      title:e.detail.value
    })
  },
  getSeach(){
    this.setData({
      pageNo:1
    })
    this.getProductList()
  },
  //获取产品
  getProductList(){
    var opt={
      label:this.data.label,
      pageNo:this.data.pageNo,
      pageSize:this.data.pageSize,
      sort:this.data.sort,
      title:this.data.title,
      typeId:this.data.typeId,
    }
    Index.apiProductList(opt).then(res=>{
      var list=[]
        var isMore=true
        if(opt.pageNo==1){
          list=res.data.list.map(item=>{
            // item.img=item.img.split(",")[0]
            return item
          })
        }else{
          var data=res.data.list.map(item=>{
            // item.img=item.img.split(",")[0]
            return item
          }),
          list=this.data.productList.concat(data)
        }
        if(res.data.list.length<opt.pageSize){
          isMore=false
        }
        this.setData({
          productList:list,
          isMore:isMore
        })
    })
  },
  onScrolltolower(){
    if(this.data.isMore){
      this.setData({
        pageNo:this.data.pageNo+1
      })
      this.getProductList()
    }
  },
  goShopDetails(e){
    var id=e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/home/productDetails/productDetails?id=${id}`,
    })
  },
  getsort(e){
    this.setData({
      sort:e.currentTarget.dataset.sort
    })
    this.getProductList()
},
getLoginBtn(){
  this.setData({
    modelPhone:true,
  })
},
closeLoginBtn(){
  this.setData({
    modelPhone:false,
  })
},
getPhoneNumber(e){
  var that=this
  var code=e.detail.code
  if(code){
    userIndex.apigetPhone({code:code}).then(res=>{
      app.globalData.isPhone=res.data.phone?true:false
      this.setData({
        isPhone:res.data.phone?true:false,
        modelPhone:false
      })
      
    })
  }
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      title:options.title
    })
    this.getProductList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})